/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.Domain;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Ignore;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestProvValidator
/*     */   extends LdapTest
/*     */ {
/*     */   private static LdapProvTestUtil provUtil;
/*     */   private static Provisioning prov;
/*     */   private static String BASE_DOMAIN_NAME;
/*     */   
/*     */   @BeforeClass
/*     */   public static void init()
/*     */     throws Exception
/*     */   {
/*  45 */     provUtil = new LdapProvTestUtil();
/*  46 */     prov = provUtil.getProv();
/*  47 */     BASE_DOMAIN_NAME = baseDomainName();
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void cleanup() throws Exception {
/*  52 */     Cleanup.deleteAll(new String[] { BASE_DOMAIN_NAME });
/*     */   }
/*     */   
/*     */   private String makeCosLimit(Cos cos, int limit) {
/*  56 */     return cos.getId() + ":" + limit;
/*     */   }
/*     */   
/*     */   private String makeFeatureLimit(String feature, int limit) {
/*  60 */     return feature + ":" + limit;
/*     */   }
/*     */   
/*     */   private Domain createDomainWithCosLimit(String domainName, Cos cos, int limit) throws Exception
/*     */   {
/*  65 */     Map<String, Object> domainAttrs = new HashMap();
/*  66 */     String cosLimit = makeCosLimit(cos, limit);
/*  67 */     domainAttrs.put("zimbraDomainCOSMaxAccounts", cosLimit);
/*     */     
/*  69 */     Domain domain = provUtil.createDomain(domainName + "." + BASE_DOMAIN_NAME, domainAttrs);
/*  70 */     return domain;
/*     */   }
/*     */   
/*     */   private Domain createDomainWithFeatureLimit(String domainName, String feature, int limit) throws Exception
/*     */   {
/*  75 */     Map<String, Object> domainAttrs = new HashMap();
/*  76 */     String featureLimit = makeFeatureLimit(feature, limit);
/*  77 */     domainAttrs.put("zimbraDomainFeatureMaxAccounts", featureLimit);
/*     */     
/*  79 */     Domain domain = provUtil.createDomain(domainName + "." + BASE_DOMAIN_NAME, domainAttrs);
/*  80 */     return domain;
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCOSMaxCreateAccount() throws Exception
/*     */   {
/*  86 */     int COS_MAX_ACCOUNTS = 2;
/*     */     
/*  88 */     Cos cos = provUtil.createCos(genCosName());
/*  89 */     Domain domain = createDomainWithCosLimit(genDomainSegmentName(), cos, 2);
/*     */     
/*  91 */     for (int i = 0; i <= 2; i++) {
/*  92 */       Map<String, Object> attrs = new HashMap();
/*  93 */       attrs.put("zimbraCOSId", cos.getId());
/*     */       
/*  95 */       boolean caughtLimitExceeded = false;
/*     */       try {
/*  97 */         acct = provUtil.createAccount("acct-" + i, domain, attrs);
/*     */       } catch (ServiceException e) { Account acct;
/*  99 */         if ("account.TOO_MANY_ACCOUNTS".equals(e.getCode())) {
/* 100 */           caughtLimitExceeded = true;
/*     */         } else {
/* 102 */           throw e;
/*     */         }
/*     */       }
/*     */       
/* 106 */       if (i < 2) {
/* 107 */         Assert.assertFalse(caughtLimitExceeded);
/*     */       } else {
/* 109 */         Assert.assertTrue(caughtLimitExceeded);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testCOSMaxModifyAccount() throws Exception {
/* 116 */     int COS_MAX_ACCOUNTS = 2;
/*     */     
/* 118 */     Cos cos = provUtil.createCos(genCosName());
/* 119 */     Domain domain = createDomainWithCosLimit(genDomainSegmentName(), cos, 2);
/*     */     Account acct;
/* 121 */     for (int i = 0; i < 2; i++) {
/* 122 */       Map<String, Object> attrs = new HashMap();
/* 123 */       attrs.put("zimbraCOSId", cos.getId());
/* 124 */       acct = provUtil.createAccount("acct-" + i, domain, attrs);
/*     */     }
/*     */     
/*     */ 
/* 128 */     Account acct = provUtil.createAccount("acct-on-default-cos", domain, null);
/*     */     
/* 130 */     boolean caughtLimitExceeded = false;
/*     */     try
/*     */     {
/* 133 */       Map<String, Object> attrs = new HashMap();
/* 134 */       attrs.put("zimbraCOSId", cos.getId());
/* 135 */       prov.modifyAttrs(acct, attrs);
/*     */     } catch (ServiceException e) {
/* 137 */       if ("account.TOO_MANY_ACCOUNTS".equals(e.getCode())) {
/* 138 */         caughtLimitExceeded = true;
/*     */       } else {
/* 140 */         throw e;
/*     */       }
/*     */     }
/* 143 */     Assert.assertTrue(caughtLimitExceeded);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testFeatureMaxCreateAccount() throws Exception {
/* 148 */     String FEATURE = "zimbraFeatureAdvancedSearchEnabled";
/* 149 */     int FEATURE_MAX_ACCOUNTS = 2;
/*     */     
/* 151 */     Domain domain = createDomainWithFeatureLimit(genDomainSegmentName(), "zimbraFeatureAdvancedSearchEnabled", 2);
/*     */     
/* 153 */     for (int i = 0; i <= 2; i++) {
/* 154 */       Map<String, Object> attrs = new HashMap();
/* 155 */       attrs.put("zimbraFeatureAdvancedSearchEnabled", "TRUE");
/*     */       
/* 157 */       boolean caughtLimitExceeded = false;
/*     */       try {
/* 159 */         acct = provUtil.createAccount("acct-" + i, domain, null);
/*     */       } catch (ServiceException e) { Account acct;
/* 161 */         if ("account.TOO_MANY_ACCOUNTS".equals(e.getCode())) {
/* 162 */           caughtLimitExceeded = true;
/*     */         } else {
/* 164 */           throw e;
/*     */         }
/*     */       }
/*     */       
/* 168 */       if (i < 2) {
/* 169 */         Assert.assertFalse(caughtLimitExceeded);
/*     */       } else {
/* 171 */         Assert.assertTrue(caughtLimitExceeded);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   @Ignore
/*     */   public void testFeatureMaxModifyAccount() throws Exception {
/* 179 */     String FEATURE = "zimbraFeatureAdvancedSearchEnabled";
/* 180 */     int FEATURE_MAX_ACCOUNTS = 2;
/*     */     
/* 182 */     Domain domain = createDomainWithFeatureLimit(genDomainSegmentName(), "zimbraFeatureAdvancedSearchEnabled", 2);
/*     */     
/* 184 */     List<String> acctIds = new ArrayList();
/* 185 */     Account acct; for (int i = 0; i <= 2; i++) {
/* 186 */       Map<String, Object> attrs = new HashMap();
/* 187 */       attrs.put("zimbraFeatureAdvancedSearchEnabled", "FALSE");
/* 188 */       acct = provUtil.createAccount("acct-" + i, domain, attrs);
/*     */     }
/*     */     
/* 191 */     for (int i = 0; i <= 2; i++) {
/* 192 */       String acctId = (String)acctIds.get(i);
/* 193 */       Map<String, Object> attrs = new HashMap();
/* 194 */       attrs.put("zimbraFeatureAdvancedSearchEnabled", "TRUE");
/* 195 */       Account acct = prov.get(Key.AccountBy.id, acctId);
/*     */       
/* 197 */       boolean caughtLimitExceeded = false;
/*     */       try {
/* 199 */         prov.modifyAttrs(acct, attrs);
/*     */       } catch (ServiceException e) {
/* 201 */         if ("account.TOO_MANY_ACCOUNTS".equals(e.getCode())) {
/* 202 */           caughtLimitExceeded = true;
/*     */         } else {
/* 204 */           throw e;
/*     */         }
/*     */       }
/*     */       
/* 208 */       if (i == 2) {
/* 209 */         Assert.assertTrue(caughtLimitExceeded);
/*     */       } else {
/* 211 */         Assert.assertFalse(caughtLimitExceeded);
/*     */       }
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestProvValidator.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */